/*
 * @Author: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @Date: 2023-03-29 14:22:54
 * @LastEditors: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @LastEditTime: 2023-03-30 18:21:15
 * @FilePath: \zs-grouting2\naive-ui-admin\src\modules\system\auth\department.service.ts
 * @Description: 权限管理
 */
import { useCurd, useForm, useTable } from '@/hooks/useCurd'
import { PaginationDto } from '@/modules/common/common.dto'
import { Injectable } from 'butterfly-decorators'
import { Application, ApplicationController } from './auth.controller'

export const tableState = useTable<Application>()
export const formState = useForm<Application>()

export const init = () => {
  tableState.columns = [{ type: 'selection' }, { title: '部门名称', key: 'applicationLogo', slot: true }]
  formState.template = [
    {
      type: 'text',
      label: '权限名称:',
      keyPath: 'departmentName',
      textProps: { placeholder: '请输入权限名称' }
    },
    {
      type: 'text',
      label: '权限描述:',
      keyPath: 'departmentDesc',
      textProps: { placeholder: '请输入权限描述' }
    },
    {
      type: 'tree',
      label: '权限',
      keyPath: 'applicationDir',
      treeProps: {
        data: [
          { label: '首页', key: 1 },
          {
            label: '系统管理',
            key: 2,
            children: [
              { label: '部门管理', key: 1 },
              { label: '用户管理', key: 2 },
              { label: '权限管理', key: 3 },
              { label: '角色管理', key: 4 },
              { label: '登录日志', key: 5 },
              { label: '操作日志', key: 6 }
            ]
          },
          {
            label: '设备',
            key: 3,
            children: [
              { label: '考勤机', key: 1 },
              { label: '测试机', key: 2 },
              { label: '台式电脑', key: 3 },
              { label: '笔记本', key: 4 },
              { label: '鼠标', key: 5 },
              { label: '显示器', key: 6 }
            ]
          },
          {
            label: '地图',
            key: 4,
            children: [
              { label: 'echarts', key: 1 },
              { label: '三维', key: 2 },
              { label: '全景', key: 3 },
              { label: '实况', key: 4 },
              { label: '静态', key: 5 },
              { label: '柱状图', key: 6 }
            ]
          },
          {
            label: '数据查询',
            key: 5,
            children: [
              { label: '动态数据', key: 1 },
              { label: '后台数据', key: 2 },
              { label: '死数据', key: 3 }
            ]
          },
          {
            label: '报告管理',
            key: 6,
            children: [
              { label: '管理1', key: 1 },
              { label: '管理2', key: 2 },
              { label: '管理3', key: 3 }
            ]
          },
          {
            label: '审批中心',
            key: 7,
            children: [
              { label: '审批中心1', key: 1 },
              { label: '审批中心2', key: 2 },
              { label: '审批中心3', key: 3 }
            ]
          },
          { label: '数据分析', key: 8 },
          { label: '报/预警管理', key: 9 },
          {
            label: '施工管理',
            key: 10,
            children: [
              { label: '施工管理1', key: 1 },
              { label: '施工管理2', key: 2 },
              { label: '施工管理3', key: 3 }
            ]
          },
          {
            label: '委托单管理',
            key: 11,
            children: [
              { label: '委托单管理1', key: 1 },
              { label: '委托单管理2', key: 2 },
              { label: '委托单管理3', key: 3 }
            ]
          }
        ],
        checkable: true
      }
    }
  ]
}

@Injectable({
  controllers: [ApplicationController]
})
export class ApplicationService {
  static applicationController: ApplicationController

  // static async getDepartmentList() {
  //   console.log(formState.template![0])
  //   formState.template![0].selectProps!.options = [
  //     { label: '开发部', value: 1 },
  //     { label: '管理部', value: 2 },
  //     { label: '检测部', value: 3 },
  //     { label: '业主部', value: 4 }
  //   ]
  // }

  static async getApplicationList({ page, size }: PaginationDto) {
    const res = await this.applicationController.findListReq({ page, size })!
    if (res.code === 200) {
      res.data.list.forEach((item) => {
        item.applicationLogo = item.applicationLogo
          ? import.meta.env.BUTTERFLY_BASE_API_URL + item.applicationLogo
          : ''
      })
      return { list: res.data.list, total: res.data.pageInfo.total }
    }
    return {
      list: [
        { departmentName: ['123'], departmentDesc: ['456'], departmentLogo: ['2023/3/31'] },
        { departmentName: ['345'], departmentDesc: ['678'], departmentLogo: ['2023/3/31'] }
      ],
      total: 0
    }
  }

  static async createOne(newData: Application) {
    const res = await this.applicationController.addOneReq(newData)!
    if (res.code == 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async updateOne(newData: Application) {
    const res = await this.applicationController.updateOneReq(newData)!
    if (res.code === 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async deleteByIds(ids: any[]) {
    const res = await this.applicationController.deleteByIdsReq(ids)!
    if (res.code === 200) {
      return { success: true, msg: '删除成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }
}

export const { r, c, u, d } = useCurd({
  r(page, size) {
    return ApplicationService.getApplicationList({ page, size })
  },
  c(newData) {
    return ApplicationService.createOne(newData)
  },
  u(newData) {
    return ApplicationService.updateOne(newData)
  },
  d(keys) {
    return ApplicationService.deleteByIds(keys)
  }
})
